#include "Options.h"
#include <string.h>
#include <stdio.h>
using namespace std;
	
Ini_Option_t ini;
Option::Option()
{
	memset (&ini, 0x0, sizeof (ini));
	bind_count = 0;
}

Option::~Option()
{
}

int Option::init(const char* config_file)
{
	if (!ini_file.open (config_file))
	{
		printf ("open config file:%s failed\n", config_file);
		return -1;
	}

	if (parse_ini () != 0)
		return -1;
	
	return 0;
}

int	Option::parse_ini()
{
	string tmp;

	tmp = ini_file.read ("logger", "LOG_MAX_SIZE");
	if (tmp.empty ())
		return -1;
	ini.log_size = atoi (tmp.c_str());

	tmp = ini_file.read ("logger", "LOG_PRENAME");
	if (tmp.empty ())
		memset (ini.log_prename, 0x0, sizeof ini.log_prename);
	else
		strncpy (ini.log_prename, tmp.c_str(), sizeof ini.log_prename);

	tmp = ini_file.read ("logger", "LOG_MAX_NUM");
	if (tmp.empty ())
		return -1;
	ini.log_num = atoi (tmp.c_str());

	tmp = ini_file.read ("logger", "LOG_PRIORITY");
	if (tmp.empty ())
		return -1;
	ini.log_priority = atoi (tmp.c_str());

	tmp = ini_file.read ("misc", "SOCKET_TIMEOUT");
	if (tmp.empty ())
		return -1;
	ini.socket_timeout = atoi (tmp.c_str());

	tmp = ini_file.read ("misc", "SOCKET_BUFSIZE");
	if (tmp.empty ())
		return -1;
	ini.socket_bufsize = atoi (tmp.c_str());

	tmp = ini_file.read ("misc", "ACCEPT_BACKLOG");
	if (tmp.empty ())
		ini.backlog = 10;
	else
		ini.backlog = atoi (tmp.c_str());

	tmp = ini_file.read ("path", "LOG_DIR");
	if (tmp.empty ())
		return -1;
	else
		strncpy (ini.log_dir, tmp.c_str(), sizeof ini.log_dir);

	tmp = ini_file.read ("net", "IP_OUT");
	if (tmp.empty ())
		return -1;
	else
		strncpy (ini.ip_out, tmp.c_str(), sizeof ini.ip_out);

	tmp = ini_file.read ("net", "PORT_OUT");
	if (tmp.empty ())
		return -1;
	else
		ini.port_out = atoi (tmp.c_str());
	
	return 0;
}

